package 滑动窗口;

import java.util.*;

/**
 * @author tmh
 * @date 2024/9/9 22:28
 * @description
 */
public class T3无重复字符的最长子串 {
    public int lengthOfLongestSubstring(String s) {
        Set<Character> result = new LinkedHashSet<>();
        int maxLen = 0;
        //定义滑动窗口的两个端点，初始值都为0，表示一开始这个窗口的大小为0
        //这个right好像和i是同步的
        int left = 0, right = 0;
        for (int i = 0; i < s.length(); i++) {
            while (result.contains(s.charAt(i))) {
                result.remove(s.charAt(left));
                left++;
            }
            result.add(s.charAt(i));
            int len = right - left + 1;
            maxLen = Math.max(len, maxLen);
            right++;
        }
        return maxLen;
    }
}
